package com.yanqu.road.dao.impl.activity.zhenhuan.thirdProcess;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.zhenhuan.thirdProcess.CrossZhenHuanThirdProcessBanquetData;
import com.yanqu.road.entity.activity.zhenhuan.thirdProcess.CrossZhenHuanThirdProcessBanquetUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;

public class CrossZhenHuanThirdProcessBanquetUserDataDaoImpl extends TempDataObjectDao<CrossZhenHuanThirdProcessBanquetUserData> {

    public static String getDataSql = "select * from `t_c_zhenhuan_third_process_banquet_user_data` where `activityId` = ? order by banquetId asc";
    public Map<Integer, Map<Long, CrossZhenHuanThirdProcessBanquetUserData>> getCrossZhenHuanThirdProcessBanquetUserDataMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Long, CrossZhenHuanThirdProcessBanquetUserData>> dataList = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossZhenHuanThirdProcessBanquetUserData data = parseData(rs);
                Map<Long, CrossZhenHuanThirdProcessBanquetUserData> joinerMap = dataList.computeIfAbsent(data.getBanquetId(), k -> new LinkedHashMap<>());
                joinerMap.put(data.getJoinUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    public static String insertSql = "INSERT INTO `t_c_zhenhuan_third_process_banquet_user_data`(`activityId`, `userId`, `banquetId`, `joinUserId`, `pos`, `createTime`) VALUES (?, ?, ?, ?, ?, ?)";
    @Override
    protected boolean addData(CrossZhenHuanThirdProcessBanquetUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBanquetId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getJoinUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPos()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected boolean updateData(CrossZhenHuanThirdProcessBanquetUserData data) {
        return false;
    }

    @Override
    protected CrossZhenHuanThirdProcessBanquetUserData parseData(ResultSet rs) throws SQLException {
        CrossZhenHuanThirdProcessBanquetUserData data = new CrossZhenHuanThirdProcessBanquetUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setBanquetId(rs.getInt("banquetId"));
        data.setJoinUserId(rs.getLong("joinUserId"));
        data.setPos(rs.getInt("pos"));
        data.setCreateTime(rs.getLong("createTime"));
        return data;
    }

    public boolean batchAdd(List<CrossZhenHuanThirdProcessBanquetUserData> dataList){
        getDb().setConnName(DbNameStrategy.getCrossDb());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (CrossZhenHuanThirdProcessBanquetUserData data : dataList) {
            int i = 1;
            Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
            para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getBanquetId()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getJoinUserId()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getPos()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(insertSql, mapList) > -1) {
            return true;
        }
        return false;
    }
}
